package cn.cvs.controller;

import cn.cvs.pojo.SysRole;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysRoleService;
import cn.cvs.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sys/role")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/list")
    public String list(Model model) {
        List<SysRole> roleList = sysRoleService.getRoleList();
        model.addAttribute("roleList", roleList);

        return "sysRole/list";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "sysRole/add";
    }

    @RequestMapping("/add")
    public String add(SysRole sysRole, HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("userSession");
        sysRole.setCreatedTime(new Date());
        sysRole.setCreatedUserId(user.getId());
        int a = sysRoleService.addRole(sysRole);
        if (a > 0) {
            return "redirect:/sys/role/list";
        } else {
            return "sysRole/add";
        }

    }

    //验证编码
    @ResponseBody
    @RequestMapping("/codeExist")
    public Object codeExist(String code) {
        Map<String, Object> map = new HashMap<>();
        List<SysRole> roleList = null;
        try {
            roleList = sysRoleService.getSysRoleByCode(code);
            if (roleList.size() > 0) {
                map.put("exist", 1);
            } else {
                map.put("exist", 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("exist", -1);
        }

        return map;
    }

    @RequestMapping("/toUpdate/{roleid}")
    public String toUpdate(@PathVariable Integer roleid, Model model) {
        System.out.println("roleid-==:" + roleid);
        SysRole sysRole = sysRoleService.getRoleById(roleid);
        model.addAttribute("sysRole", sysRole);
        return "sysRole/update";
    }

    @RequestMapping("/update")
    public String update(SysRole sysRole, HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("userSession");
        sysRole.setUpdatedTime(new Date());
        sysRole.setUpdatedUserId(user.getId());
        int a = sysRoleService.updateRoleBySet(sysRole);
        if (a > 0) {
            return "redirect:/sys/role/list";
        } else {
            return "sysRole/update";
        }

    }

    @ResponseBody
    @RequestMapping("/del/{roleid}")
    public Object del(@PathVariable Integer roleid) {
        System.out.println("delroleid-====:" + roleid);
        Map<String, Object> map = new HashMap<>();
        //先查询是否有用户引用该角色
        int b = -1;
        int a = -1;
        try {
            b = sysUserService.getUserByRoleId(roleid);
            if (b > 0) {
                map.put("delResult", "false");
            } else {
                a = sysRoleService.delRole(roleid);
                if (a > 0) {
                    map.put("delResult", "true");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", "notexist");
        }
        return map;
    }


}
